﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BAL.EstRec;
using System.Windows;
using System.Text.RegularExpressions;

namespace Nav3.PasteExcelData
{
    public class CPFClipboardConverter
    {
        private string clipboardData;
        private CapturaPivotFDV cpf;

        public CPFClipboardConverter()
        {
        }


        public string convertCilpboardToString(IDataObject data)
        {
            string clipboardData = "";
            try
            {
                if (data.GetDataPresent(DataFormats.Text))
                {
                    clipboardData = (String)data.GetData(DataFormats.Text);
                }
            }
            catch (Exception ex)
            {
                clipboardData = "";
            }

            return clipboardData;
        }
        
        public CollectionsPivotFDV Convert(IDataObject data)
        {
            CollectionsPivotFDV cpf=null;
            string clipboardData;

            clipboardData = this.convertCilpboardToString(data);

            try
            {
                string[] lines = Regex.Split(clipboardData, "\r\n");
                var res = lines.Select(d =>
                {
                    CapturaPivotFDV _cpf = null;
                    try
                    {
                        _cpf = new CapturaPivotFDV(new Pos(d.Split('\t')[0].ToString().Trim(), d.Split('\t')[0].ToString().Trim())
                                                         , new Recurso(d.Split('\t')[1].ToString().Trim(), d.Split('\t')[1].ToString().Trim())
                                                         , System.Convert.ToInt32(d.Split('\t')[2].ToString()));
                    }
                    catch (Exception ex)
                    {
                        ;
                    }
                    return _cpf;
                });

                var res2 = (from o in res
                           where o != null
                           select o).ToList();
                cpf = new CollectionsPivotFDV(res2);
            }
            catch (Exception ex)
            {
                //No se pudo convertir por que el formato no es coincidente
                cpf = null;
            }

            return cpf;
        }
    }
}
